/*
 * @(#)MaintDmsGroupRoleFolderAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsObjectHomeFolder;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.dms.dao.DmsObjectHomeFolderDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  MaintDmsGroupRoleFolderAction.java

  This class is for maint dms paper document.

    @author          Jenny Li
    @company         DCIVision Limited
    @creation date   14/10/2004
    @version         $Revision: 1.6 $
*/

public class MaintDmsGroupRoleFolderAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.6 $";
  protected String parentID = "";
  protected String rootID = "";

    
  public MaintDmsGroupRoleFolderAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsObjectHomeFolderDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_ADMIN);
  }
  
  /**
   *  Method execute() - generaic function of result list handling               
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
  	//  Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }
    MaintDmsGroupRoleFolderForm actionForm=(MaintDmsGroupRoleFolderForm)form;
    parentID = actionForm.getParentID();
    rootID = actionForm.getRootID();
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    DmsDocument documentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(actionForm.getRootID()));
    if (Utility.isEmpty(parentID) || "null".equals(parentID) ||
        Utility.isEmpty(rootID) || "null".equals(rootID)) {
      DmsRoot personalRoot = rootRetrievalManager.getRootObjectByOwnerID(sessionContainer.getUserRecordID());
      documentRoot = docRetrievalManager.getRootFolderByRootID(personalRoot.getID());
      parentID = TextUtility.formatIntegerObj(documentRoot.getParentID());
      rootID = TextUtility.formatIntegerObj(personalRoot.getID());
      actionForm.setRootID(rootID);
      actionForm.setParentID(parentID);
    }
    String path=docRetrievalManager.getLocationPath(TextUtility.parseIntegerObj(actionForm.getParentID()));
    request.setAttribute("location", path);
    return super.execute(mapping,form,request,response);
  }
  
  /**
   *  Method selectRecord() - rewrite the parent function.             
   *
   *  @param      mapping                   ActionMapping from struts
   *  @param      form                      ActionForm from struts
   *  @param      request                   HttpServletReuqest
   *  @param      respond                   HttpServletRespond
   *  @throws     ApplicationException      Throws ApplicationException
   */
  public void selectRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    MaintDmsGroupRoleFolderForm actionForm=(MaintDmsGroupRoleFolderForm)form;
    DmsObjectHomeFolderDAObject dmsObjectHomeFolderDAO=new DmsObjectHomeFolderDAObject(sessionContainer,conn); 
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
    Integer ID=TextUtility.parseIntegerObj(actionForm.getID());
    DmsObjectHomeFolder dmsObjectHomeFolder=(DmsObjectHomeFolder)dmsObjectHomeFolderDAO.getObjectByID(ID);
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    String location=docRetrievalManager.getLocationPath(dmsObjectHomeFolder.getDocumentID());
    String memberName="";
    if(DmsObjectHomeFolder.MEMBER_TYPE_GROUP.equals(dmsObjectHomeFolder.getMemberType())){
      UserGroup userGroup=(UserGroup)userGroupDAO.getObjectByID(dmsObjectHomeFolder.getMemberID());
      memberName=userGroup.getGroupName();
    }else if(DmsObjectHomeFolder.MEMBER_TYPE_ROLE.equals(dmsObjectHomeFolder.getMemberType())){
      UserRole userRole=(UserRole)userRoleDAO.getObjectByID(dmsObjectHomeFolder.getMemberID());
      memberName=userRole.getRoleName();
    }
    dmsObjectHomeFolder.setLocation(location);
    dmsObjectHomeFolder.setMemberName(memberName);
    request.setAttribute("location",location);
    actionForm.setFormData(dmsObjectHomeFolder);
  }
  
  /**
   *  Method updateRecord() - rewrite the parent function.             
   *
   *  @param      mapping                   ActionMapping from struts
   *  @param      form                      ActionForm from struts
   *  @param      request                   HttpServletReuqest
   *  @param      respond                   HttpServletRespond
   *  @throws     ApplicationException      Throws ApplicationException
   */
  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    MaintDmsGroupRoleFolderForm actionForm=(MaintDmsGroupRoleFolderForm)form;
    DmsObjectHomeFolderDAObject dmsObjectHomeFolderDAO=new DmsObjectHomeFolderDAObject(sessionContainer,conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
    DmsObjectHomeFolder dmsObjectHomeFolder=(DmsObjectHomeFolder)dmsObjectHomeFolderDAO.getObjectByID(TextUtility.parseIntegerObj(actionForm.getID()));    
    if(!Utility.isEmpty(actionForm.getDestinationID()) && !"0".equals(actionForm.getDestinationID())){   
      dmsObjectHomeFolder.setDocumentID(TextUtility.parseIntegerObj(actionForm.getDestinationID())); 
      dmsObjectHomeFolder=(DmsObjectHomeFolder)dmsObjectHomeFolderDAO.updateObject(dmsObjectHomeFolder);
    }
    
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    String location=actionForm.getDestinationPath();
    String memberName="";
    if(DmsObjectHomeFolder.MEMBER_TYPE_GROUP.equals(dmsObjectHomeFolder.getMemberType())){
      UserGroup userGroup=(UserGroup)userGroupDAO.getObjectByID(dmsObjectHomeFolder.getMemberID());
      memberName=userGroup.getGroupName();
    }else if(DmsObjectHomeFolder.MEMBER_TYPE_ROLE.equals(dmsObjectHomeFolder.getMemberType())){
      UserRole userRole=(UserRole)userRoleDAO.getObjectByID(dmsObjectHomeFolder.getMemberID());
      memberName=userRole.getRoleName();
    }
    dmsObjectHomeFolder.setLocation(location);
    dmsObjectHomeFolder.setMemberName(memberName);
    request.setAttribute("location",location);
    actionForm.setFormData(dmsObjectHomeFolder);
  }
  
}